import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.antlr.v4.runtime.*;
import org.antlr.v4.gui.Trees;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public class Main {

    public static void main(String[] args) throws IOException{
	// write your code here
        InputStream is = args.length > 0 ? new FileInputStream(args[0]) : System.in;
        String fileName = args[0];

        ANTLRInputStream input = new ANTLRInputStream(is);
        CmmLexer lexer = new CmmLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CmmParser parser = new CmmParser(tokens);
        ParseTree rule=parser.program();
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(new CmmBaseListener(),rule);

        if(parser.getNumberOfSyntaxErrors()!=0){
            System.out.println("The grammer error must be solved");
            System.exit(0);
        }

        try{
            File directory = new File(".");
            System.out.println("your current path is:"+directory.getCanonicalPath());
            File[] files=directory.listFiles();
            String[] neceFiles={"GoAsm.Exe","GoLink.exe"};
            for(String fileString:neceFiles){
                for(int i=0;i<files.length;i++){

                    if(fileString.equals(files[i].getName())){
                        System.out.println(files[i].getName()+" is found");
                        break;
                    }
                    if(i==files.length-1){
                        System.out.println("Error: Can't find file :"+fileString);
                        System.exit(0);
                    }
                }
            }
            System.out.println("Sucess");
            System.out.println("");

            System.out.println("Begin to produce asm file...............");
            //写入asm文件
            PrintStream out=new PrintStream(fileName.substring(0, fileName.length()-4)+".asm");
            IRProducer.printfCode(out);
            out.flush();
            out.close();
            //System.out.println("Sucess..................................");
            System.out.println("");
            //System.out.println("Making obj file.........................");
            //调用编译器生成obj文件
            Process p1=Runtime.getRuntime().exec("GoAsm /x64 "+fileName.substring(0, fileName.length()-4)+".obj "+
                    fileName.substring(0, fileName.length()-4)+".asm");
            BufferedReader bf1 =new BufferedReader(new InputStreamReader(p1.getInputStream())) ;
            String line=null;
            while ((line=bf1.readLine()) != null) {
                //System.out.println(line);
            }
            bf1.close();
            p1.waitFor();
            System.out.println("");

            System.out.println("Making exe file.........................");
            //调用链接器生成exe文件
            //System.out.println(directory.getCanonicalPath()+"\\msvcrt.dll");
            Process p2=Runtime.getRuntime().exec("GoLink /console "+fileName.substring(0, fileName.length()-4)+".obj"+" kernel32.dll msvcrt.dll");
            BufferedReader bf2 =new BufferedReader(new InputStreamReader(p2.getInputStream())) ;
            String line2=null;
            while ((line2=bf2.readLine()) != null) {
                System.out.println(line2);
            }
            bf2.close();
            p2.waitFor();
            System.out.println("");


        } catch (IOException | InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("The program runs unsucessfully");
        }



    }
}
